from coin.exchange.base.kr_rest.feed_base import BookHolder
from coin.exchange.base.kr_ws.ws_parser_base import WsFeedHandlerBase
from coin.exchange.upbit_v1.kr_ws.ws_client import UpbitWSClient
import coin.exchange.upbit_v1.kr_rest.product as exchange_product


class UpbitWsFeedUpdater(WsFeedHandlerBase):
  def __init__(self, feed_base):
    self.feed_base = feed_base

  def on_book(self, msg):
    diff_exchange_fetched = abs(msg.fetched_time - msg.exchange_time)

    if diff_exchange_fetched < 7e+9 and diff_exchange_fetched > 5e+9:
      self.feed_base.set_book_proto(msg)
    else:
      raise ValueError(diff_exchange_fetched)

  def on_ticker(self, msg):
    pass

  def on_status(self, msg):
    pass

  def on_trade(self, msg):
    pass


class UpbitWsFeedBase(BookHolder):
  ProductType = exchange_product.UpbitProduct
  is_price_float = True

  def __init__(self, product):
    BookHolder.__init__(self, product)
    self.ws_updater = UpbitWsFeedUpdater(self)
    self.ws_client = UpbitWSClient(UpbitWSClient.RequestType.FromSingleProduct(product),
                                   self.ws_updater)
    self.ws_client.start()

  def query_book(self):
    pass
